<?php

abstract class a_base_controller extends Controller implements i_base_controller, i_handle_posts {

    protected $global_smarty_vars = array();
    protected $post_handling_enabled = FALSE;
    protected $post_vars = array();
    protected $run_auto_view = TRUE;
    protected $render_partially = FALSE;
    protected $global_msgs = array();
    protected $process_global_before_view = FALSE;

    public function __construct($post_handling_enabled=FALSE) {
        parent::__construct();
        $this->post_handling_enabled = $post_handling_enabled;

        $this->_call_hook();

        $this->add_global_var("current_module", $this->ci()->current_module);
        $this->add_global_var("current_controller", $this->ci()->current_controller);
        $this->add_global_var("images", base_url() . "application/modules/" . $this->ci()->current_module . "/images");
        $this->add_global_var("css", base_url() . "application/modules/" . $this->ci()->current_module . "/css");
        $this->add_global_var("js", base_url() . "application/modules/" . $this->ci()->current_module . "/js");
        $this->add_global_var("core_images", base_url() . "application/modules/core/images");
        $this->add_global_var("product_images", base_url() . "application/modules/product/images");
        $this->add_global_var("core_css", base_url() . "application/modules/core/css");
        $this->add_global_var("core_js", base_url() . "application/modules/core/js");
        $this->add_global_var("base_url", base_url());
        $this->add_global_var("base_index_url", base_url() . $this->config->item('index_page'));
        $this->add_global_var("post_vars", $this->list_post_vars());
        $this->add_global_var("do_partial_endering", $this->do_partial_rendering());
    }

    private function _call_hook() {
        $EXT = & load_class('Hooks');
        $EXT->_call_hook('base_controller_constructor');
    }

    protected function _process_global_variables() {
        $keys = array_keys($this->global_smarty_vars);
        if (count($keys)) {
            foreach ($keys as $key) {
                $this->assign($key, $this->global_smarty_vars[$key]);
            }
        }

        $this->smarty()->assign_by_ref("msgs", $this->global_msgs);
    }

    protected function add_global_var($name, $value) {
        $this->global_smarty_vars[$name] = $value;
    }

    protected function set_theme($name) {
        $this->ci()->theme = $name;
        $this->smarty()->set_theme($name);
    }

    public function ci() {
        return get_instance();
    }

    public function assign($name, $value) {
        $this->smarty_wrapper->assign($name, $value);
    }

    public function smarty() {
        return $this->smarty_wrapper;
    }

    public function do_partial_rendering() {
        if ($this->input->post(i_handle_posts::RENDER_PARTIAL)) {
            $this->render_partially = TRUE;
        } else {
            $this->render_partially = FALSE;
        }
        return $this->render_partially;
    }

    public function is_ajax() {
        if ($this->input->post(i_handle_posts::IS_AJAX)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function view($name=NULL, $process_global_again=FALSE) {
        $this->batch_assign();
        if ($this->process_global_before_view || $process_global_again)
            $this->_process_global_variables();
        $view_name = $name ? $name : get_class($this);
        $this->assign("current_view", $view_name);
        is_a($this, "a_admin_controller") ? $this->smarty()->admin_view($view_name) : $this->smarty()->view($view_name);
    }

    /**
     * Returns true if the request is a post from user
     * @return bool
     */
    public function is_post() {
        return count($_POST) > 0 ? TRUE : FALSE;
    }

    /**
     * If returns true then the automatic functiona remapping will be enabled
     * @return bool
     */
    public function is_handling_post_enabled() {
        return $this->post_handling_enabled;
    }

    /**
     * Get the current operation name coming from the form hidden field. $_POST['op_name']
     * @return string|NULL
     */
    public function get_op_name() {
        if ($this->input->post(i_handle_posts::OP_CODE)) {
            return $this->input->post(i_handle_posts::OP_CODE);
        } else {
            return NULL;
        }
    }

    /**
     * Saves all POST vars inside an array to post them back.
     * @return void
     */
    public function list_post_vars() {
        $keys = array_keys($_POST);
        if (count($keys)) {
            foreach ($keys as $key) {
                $this->post_vars[$key] = $this->input->post($key);
            }
        }
        return $this->post_vars;
    }

    /**
     * This basically maps all function calls to acontroller.And also takes responsibility
     * of rendering age to some extend if the class variable $fun_auto_view is set to TRUE.
     * Steps
     *  1.Sees if the auto post handling is enabled.
     *  2.if enabled then call the function found in the op_code POST var
     *  3.if not call the function directly..no mapping is done in this case
     *  4.now call the batch_assign() and complete data assigning on smarty for the current controller
     *  5.now see if this is an ajax call..in that case return the just called function's output
     *  6.else render the page by clling view function
     *
     */
    public function _remap($method) {
        $this->_process_global_variables();
        $this->batch_assign();
        if ($this->is_handling_post_enabled()) {
            if (($op_name = $this->get_op_name()) != NULL) {
                $return = call_user_func_array(array(&$this, strtolower($op_name)), array_slice($this->uri->rsegments, 2));
            } else {
                $return = call_user_func_array(array(&$this, $method), array_slice($this->uri->rsegments, 2));
            }
        } else {
            $return = call_user_func_array(array(&$this, $method), array_slice($this->uri->rsegments, 2));
        }
//        $this->_process_global_variables();
//        $this->batch_assign();
        if ($this->run_auto_view) {
            if ($this->is_ajax()) {
                echo $return;
            } else {
                $this->view();
            }
        } else {
            if ($this->is_ajax())
                echo $return;
        }
    }

    protected function add_global_msg($msg) {
        $this->global_msgs[] = $msg;
    }
    
    protected function do_redirect($msg='', $delay=3, $uri='') {
        $this->run_auto_view = FALSE;
        $delay = $delay * 1000;
        echo "
        <div style= \"border: 1px solid;margin: 10px 0px;padding:15px 15px 15px 15px;color: #00529B;background-color: #BDE5F8;font-size:14px\">
            $msg
        </div>";
        if (!preg_match('#^https?://#i', $uri)) {
            $uri = site_url($uri);
        }
        $script = "setTimeout(\"location.href = '$uri';\",$delay);";
        echo "<script type='text/javascript'> $script</script>";
        exit();
    }

}

?>